// pages/shareActive/index.js
const {
  post
} = getApp();
Page({
  data: {
    show: false,
    regionList: [{
      "id": "",
      "text": "全部",
      "name": "全部",
      "longitude": 0,
      "latitude": 0
    }],
    params: {
      name: '',
      employee_company_id: "",
      longitude: 0,
      latitude: 0,
      page: 1,
      pageSize: 20
    },
    dataList: [],
    region: "全部",
    showLoading: true,
    showPage: false,
    showCon: false
  },
  // 获取数据
  getList() {
    if (this.data.showLoading) {
      post("/spread/activity/list", this.data.params).then(res => {
        if (res.result == 1) {
          const dataList = this.data.dataList.concat(res.data);
          this.setData({
            dataList,
            showPage: true
          });
          if (res.data.length < this.data.params.pageSize) {
            this.setData({
              showLoading: false
            })
          }
        };
      });
    }
  },
  // 地区确定
  changeRegion(e) {
    this.setData({
      "region": e.detail.value.name,
      "params.employee_company_id": e.detail.value.id,
      "params.longitude": e.detail.value.longitude,
      "params.latitude": e.detail.value.latitude,
      "show": false,
      "showLoading": true,
      "showPage": false,
      "dataList": []
    });
    this.getList();
  },
  // 显示地区列表
  getRegionList() {
    const _this = this;
    wx.getLocation({
      type: 'wgs84',
      success(res) {
        _this.setData({
          [`regionList[0].longitude`]: res.longitude,
          [`regionList[0].latitude`]: res.latitude,
        });
        post("/spread/station/list", {
          longitude: res.longitude,
          latitude: res.latitude,
        }).then(res => {
          if (res.result == 1) {
            if (res.data.length == 0) {
              wx.showToast({
                title: '周围暂时没有活动',
                icon: 'none',
                duration: 2000
              })
              return;
            }
            const arry = res.data.map((item) => {
              return {
                ...item,
                text: item.name
              }
            })
            _this.setData({
              "regionList": _this.data.regionList.concat(arry),
              "params.longitude": res.data[0].longitude,
              "params.latitude": res.data[0].latitude,
            });
            _this.getList();
          };
        });
      },
      fail(err) {
        wx.getSetting({
          withSubscriptions: true,
          success: (res) => {
            if (!res.authSetting['scope.userLocation']) {
              _this.setData({
                showCon: true
              })
            }
          }
        });
      }
    });
  },
  // 关闭
  changeModalCancel() {
    this.setData({
      showCon: false
    })
  },
  // 显示地区列表
  showRegion() {
    this.setData({
      show: true
    })
  },
  // 关闭地区列表
  closeRegion() {
    this.setData({
      show: false
    })
  },
  // 搜索活动
  searchValue(e) {
    this.setData({
      "params.name": e.detail,
      "showPage": false
    })
    this.getList();
  },
  // 上拉加载
  onReachBottom: function (e) {
    this.setData({
      "params.page": this.data.params.page += 1
    });
    this.getList();
  },
  // 输入框确定
  searchValue(e) {
    this.setData({
      "params.page": 1,
      "dataList": [],
      "params.name": e.detail,
      "showLoading": true,
      "showPage": false
    });
    this.getList();
  },
  // 分享
  onShareAppMessage: function (e) {
    return {
      title: e.target.dataset.title,
      path: `/pages/activityDetails/index?activity_code=${e.target.dataset.activity_code}&share_member=${e.target.dataset.share_member}`,
      imageUrl: e.target.dataset.activity_img,
      success: function (res) {
        console.log("分享成功:" + JSON.stringify(res));
      },
      fail: function (res) {
        console.log("分享失败:" + JSON.stringify(res));
      }
    }
  },
  onShow: function () {
    this.getRegionList();
    // 控制分享按钮
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline'],
      success(res) {
        console.log('showShareMenu', res);
      }
    });
  }
})